package com.project.ujob.controllers;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.LocaleResolver;

@Controller
public class InternationalController {
	@Autowired
	private MessageSource messageSource;
//	@Autowired
//	private LocaleResolver localeResolver;
	// for international
	@GetMapping(value = "/sample")
	public String sample() {
		return "sample";
	}

	@RequestMapping(value = "/changLang")
	public String i18nChange(@RequestParam("lang") String lang, Model model,HttpServletRequest request, HttpServletResponse response) {
		System.out.println("lang =>" + lang);
		Locale locale =new Locale(lang.split("_")[0],lang.split("_")[1]);
	//	localeResolver.setLocale(request, response, locale);
	//	Locale locale = LocaleContextHolder.getLocale();
		String key = messageSource.getMessage("key", null, locale);
		String message = messageSource.getMessage("message", null, locale);
		System.out.println("---->" + key);
		System.out.println("---->" + message);
		model.addAttribute("key", key);
		return "sample";

	}
}
